var util = (function util() {
    var validator = (function validator() {
        var regs = {
            phone: /^((\+?86)|(\(\+86\)))?(1[0-9]{10})$/
        };

        return {
            isEmpty: function isEmpty(value) {
                return !value;
            },
            isPhone: function isPhone(phone) {
                return regs.phone.test(phone);
            },
            isInRange: function isInRange(number) {
                number = parseInt(number, 10);
                return !!(number > 0 && number <= 1000000);
            }
        };
    })();

    var pageManager = (function pageManager() {
        return {
            go: function go(selector) {
                $('#container').html($(selector).html());
            }
        };
    })();

    var fastClick = function fastClick() {
        var supportTouch = (function supportTouch() {
            try {
                document.createEvent('TouchEvent');
                return true;
            } catch (e) {
                return false;
            }
        })();
        var old$On = $.fn.on;

        $.fn.on = function() {
            if (/click/.test(arguments[0]) && typeof arguments[1] == 'function' && supportTouch) {
                _old$On.apply(this, ['touchstart', function(e) {
                    touchStartY = e.changedTouches[0].clientY;
                }]);
                _old$On.apply(this, ['touchend', function(e) {
                    if (Math.abs(e.changedTouches[0].clientY - touchStartY) > 10) {
                        return;
                    }

                    e.preventDefault();
                    callback.apply(this, [e]);
                }]);
            } else {
                _old$On.apply(this, arguments);
            }

            return this;
        };
    };

    var setFooter = function setFooter(selector) {
        var $foot = $(selector);

        if ($foot.length < 1) {
            return;
        }

        if ($foot.position().top + $foot.height() < $(window).height()) {
            $foot.addClass('j_bottom');
        } else {
            $foot.removeClass('j_bottom');
        }
    };

    var getQueryString = function getQueryString(name) {
        var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
            var r = window.location.search.substr(1).match(reg);

            if (r !== null) {
                return unescape(r[2]);
            }

            return null;
    };

    var toggleErrMsg = function toggleErrMsg(selector, message) {
        var $err = $(selector);

        if ($err.css('display') !== 'none') {
            return;
        }

        $err.text(message).css('display', 'block');
        setTimeout(function onTimeout() {
            $err.css('display', 'none');
        }, 2000);
    };

    return {
        isEmpty: validator.isEmpty,
        isPhone: validator.isPhone,
        isInRange: validator.isInRange,
        go: pageManager.go,
        fastClick: fastClick,
        setFooter: setFooter,
        getQueryString: getQueryString,
        toggleErrMsg: toggleErrMsg
    };
})();

;(function($) {
    $.fn.serializeObject = function serializeObject() {
        var result = {};
        this.serializeArray().forEach(function(item) {
            result[item.name] = item.value;
        });
        return result;
    };
})(jQuery);